﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;



namespace ServeNHL.Models
{
    [MetadataType(typeof(eventCatMetadata))]
    public partial class EVENT_CAT
    {

        /// <summary>
        /// updates or inserts based on EVENT CAT ID 
        /// </summary>
        /// <param name="evtCat"></param>
        /// <returns>eventCatID</returns>
        public int saveEventCat(EVENT_CAT evtCat)
        {
            int eventCatID = 0;

            if(evtCat.EVENT_CAT_ID > 0)
            {
                updateEventCat(evtCat);
            }
            else
            {
                insertEventCat(evtCat);
            }

            eventCatID = evtCat.EVENT_CAT_ID;
            
            return eventCatID;
        } // End of public int saveEventCat(eventViewModel evtCat, VCLViewModel vcl)

        /// <summary>
        /// inserts EVENTCAT into DB called by SaveEventCat
        /// </summary>
        /// <param name="evtCat"></param>
        /// <returns>eventCatID</returns>
        public int insertEventCat(EVENT_CAT evtCat)
        {
            int eventCatID = 0;

            using (var dbconn = new fclick_dbEntities())
            {
                var tempCat = new EVENT_CAT
                {
                    EVENT_CAT_NAME = evtCat.EVENT_CAT_NAME,
                    EVENT_CAT_DESC = evtCat.EVENT_CAT_DESC,
                    LOGO = evtCat.LOGO,
                    STATUS = evtCat.STATUS,
                    CREATED_DATE = Helpers.createDate.getCreateDate(),
                    NEED_FLAG = evtCat.NEED_FLAG,
                    VCL_ID = evtCat.VCL_ID,
                    LAST_MOD_DATE = Helpers.createDate.getCreateDate(),
                };

                dbconn.EVENT_CAT.Add(tempCat);
                dbconn.SaveChanges();

                eventCatID = evtCat.EVENT_CAT_ID;

            }// end of  using (var dbconn = new fclick_dbEntities())
            return eventCatID;

        }// end of  public int insertEventCat(EVENT_CAT evtCat)


        /// <summary>
        /// updates Event Cat in DB, called by Save
        /// </summary>
        /// <param name="evtCat"></param>
        /// <returns>eventCatID</returns>
        public int updateEventCat(EVENT_CAT evtCat) {
            int eventCatID = 0;

            using (var dbconn = new fclick_dbEntities())
            {
                var tempCat = new EVENT_CAT();

                if (!String.IsNullOrEmpty(evtCat.EVENT_CAT_NAME))
                {
                    tempCat.EVENT_CAT_NAME = evtCat.EVENT_CAT_NAME;
                }

                if (!String.IsNullOrEmpty(evtCat.EVENT_CAT_DESC))
                {
                    tempCat.EVENT_CAT_DESC = evtCat.EVENT_CAT_DESC;
                }

                if (!String.IsNullOrEmpty(evtCat.LOGO))
                {
                    tempCat.LOGO = evtCat.LOGO;
                }

                if (!String.IsNullOrEmpty(evtCat.STATUS))
                {
                    tempCat.STATUS = evtCat.STATUS;
                }

                tempCat.NEED_FLAG = evtCat.NEED_FLAG;

                if (evtCat.VCL_ID >= 0)
                {
                    tempCat.VCL_ID = evtCat.VCL_ID;
                }

                tempCat.CREATED_DATE = evtCat.CREATED_DATE;

                dbconn.EVENT_CAT.Add(tempCat);
                dbconn.SaveChanges();

                eventCatID = evtCat.EVENT_CAT_ID;
            }// end of   using (var dbconn = new fclick_dbEntities())

            return eventCatID;
        }// end of public int updateEventCat(EVENT_CAT evtCat) 

        public List<EVENT_CAT> getAllCategories()
        {
            var list = new List<EVENT_CAT>();

            using (var dbconn = new fclick_dbEntities())
            {
                list = dbconn.EVENT_CAT.OrderBy(ec => ec.STATUS).ToList();
            }

            return list;
        }

        public static List<EVENT_CAT> getCategoriesDropdown()
        {
            var cat = new EVENT_CAT();
            return cat.getAllCategories();
        }

    }// end of public partial class eventCat


}